package operation;

import book.Book;
import book.BookList;

import java.util.Scanner;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 黎鹤舞
 * Date: 2023-11-21
 * Time: 15:55
 */

//这里是增加图书的操作类
public class AddOperation implements IOperation {
    public void work(BookList bookList) {

        //currentNum 表示的是当前书架上书籍的数量
        int currentNum = bookList.getBookNum();
        if(currentNum == 10) {
            System.out.println("书架已满!!!!");
        }

        System.out.println("新增图书:");
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入你要新增书籍的名称:");
        String name = scanner.nextLine();

        //这里可以判断一下，如果输入的书名在书架上已经有了，则提醒
        for (int i = 0; i < currentNum; i++) {
            //逐个判断name是否等于books的第i个元素
            Book book = bookList.getBooks(i);
            if(book.getName().equals(name)) {
                System.out.println("这本书已经存在于书架上了!!!!");
                return;
            }
        }

        System.out.println("请输入你要新增书籍的作者:");
        String author = scanner.nextLine();
        System.out.println("请输入你要新增书籍的价格:");
        int price = scanner.nextInt();
        System.out.println("请输入你要新增书籍的种类:");
        String type = scanner.nextLine();

        //把这个book信息放到books 的下一个空数组元素中
        Book book = new Book(name,author,price,type);
        bookList.setBooks(currentNum,book);
        bookList.setBookNum(currentNum + 1);
    }
}
